﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace EmployeeCheckIn
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null && Session["Pass"] != null)
                {
                    if (Session["emp_code"] != null)
                    {
                        TextBox3.Text = Session["emp_code"].ToString();
                        BindGridViewShowList();
                        Session["emp_code"] = null;
                    }
                    else if (Session["editReload"] != null)
                    {
                        SerchDB loaddate = new SerchDB();
                        DataTable loadtb = loaddate.timeofdate();
                        DataTable showdata = loaddate.SearchALL();
                        HidPenalty.Value = loadtb.Rows[0]["LATE_MONEY"].ToString();
                        HidLateTime.Value = loadtb.Rows[0]["TIME_OF_LATE"].ToString();
                        GridView1.DataSource = showdata;
                        GridView1.DataBind();
                        lb_warn.Visible = false;
                        GridView1.Visible = true;
                        Session["editReload"] = null;
                    }
                    else
                    {
                        SerchDB loaddate = new SerchDB();
                        DataTable loadtb = loaddate.timeofdate();
                        DataTable showdata = loaddate.SearchALL();
                        HidPenalty.Value = loadtb.Rows[0]["LATE_MONEY"].ToString();
                        HidLateTime.Value = loadtb.Rows[0]["TIME_OF_LATE"].ToString();
                        if (showdata.Rows.Count == 0)
                        {
                            Warnning();
                        }
                        else
                        {
                            GridView1.DataSource = showdata;
                            GridView1.DataBind();
                            lb_warn.Visible = false;
                            GridView1.Visible = true;
                        }
                    }
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }

        private void BindGridViewShowList()
        {
            SerchDB loaddate = new SerchDB();
            DataTable loadtb = loaddate.timeofdate();
            DataTable showdata = loaddate.NotDay(TextBox3.Text);
            if (showdata.Rows.Count == 0)
            {
                Warnning();
            }
            else
            {
                GridView1.DataSource = showdata;
                GridView1.DataBind();
                lb_warn.Visible = false;
                GridView1.Visible = true;
            }
            HidPenalty.Value = loadtb.Rows[0]["LATE_MONEY"].ToString();
            HidLateTime.Value = loadtb.Rows[0]["TIME_OF_LATE"].ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {
                CutString Cutdate = new CutString();
            if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text != "")
            {
                SerchDB loaddate = new SerchDB();
                DataTable showdata = loaddate.NotDay(TextBox3.Text);
                if (showdata.Rows.Count==0)
                {
                    Warnning();
                }
                else
                {
                    GridView1.DataSource = showdata;
                    GridView1.DataBind();
                    lb_warn.Visible = false;
                    GridView1.Visible = true;
                }
            }
            else if (TextBox1.Text != "" && TextBox2.Text == "" && TextBox3.Text == "")
            {
                string startDateSerch = Cutdate.dtm(TextBox1.Text, "").ToString("yyyy-MM-dd HH:mm",new System.Globalization.CultureInfo("en-US"));
                string lastDateSerch =  Cutdate.dtm(TextBox1.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                SerchDB loaddate = new SerchDB();
                DataTable showdata = loaddate.DateOnly(startDateSerch,lastDateSerch);
                if (showdata.Rows.Count == 0)
                {
                    Warnning();
                }
                else
                {
                    GridView1.DataSource = showdata;
                    GridView1.DataBind();
                    lb_warn.Visible = false;
                    GridView1.Visible = true;
                }
            }
            else if (TextBox1.Text == "" && TextBox2.Text != "" && TextBox3.Text == "")
            {
                string startDateSerch = Cutdate.dtm(TextBox2.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                string lastDateSerch = Cutdate.dtm(TextBox2.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                SerchDB loaddate = new SerchDB();
                DataTable showdata = loaddate.DateOnly(startDateSerch, lastDateSerch);
                if (showdata.Rows.Count == 0)
                {
                    Warnning();
                }
                else
                {
                    GridView1.DataSource = showdata;
                    GridView1.DataBind();
                    lb_warn.Visible = false;
                    GridView1.Visible = true;
                    
                }
            }
            else if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text == "")
            {
                DateTime startDateSerch = Cutdate.dtm(TextBox1.Text, "00:00:00");
                DateTime lastDateSerch  = Cutdate.dtm(TextBox2.Text, "23:59:59");
                SerchDB loaddate = new SerchDB();
                DataTable showdata = loaddate.DateOnly(startDateSerch, lastDateSerch);
                if (showdata.Rows.Count == 0)
                {
                    Warnning();
                }
                else
                {
                    GridView1.DataSource = showdata;
                    GridView1.DataBind();
                    lb_warn.Visible = false;
                    GridView1.Visible = true;
                    
                }
            }
            else if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text == "")
            {
                SerchDB loaddate = new SerchDB();
                DataTable showdata = loaddate.SearchALL();
                GridView1.DataSource = showdata;
                if (showdata.Rows.Count == 0)
                {
                    Warnning();
                }
                else
                {
                    GridView1.DataBind();
                    lb_warn.Visible = false;
                    GridView1.Visible = true;
                    
                }
            }
            else if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "")
            {
                GridView1.Visible = true;
              
                lb_warn.Visible = false;
                string startDateSerch = Cutdate.dtm(TextBox1.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                string lastDateSerch = Cutdate.dtm(TextBox2.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm:ss", new System.Globalization.CultureInfo("en-US"));
                SerchDB gviewdb = new SerchDB();
                DataTable showdata = gviewdb.Gviewdb(startDateSerch, lastDateSerch, TextBox3.Text);
                if (showdata.Rows.Count == 0)
                {
                    Warnning();
                }
                else
                {
                    GridView1.DataSource = showdata;
                    GridView1.DataBind();
                }
            }
            else
            {
                lb_warn.Visible = true;
                GridView1.Visible = false;
                
            }

            }
            else
            {
                Response.Redirect("Default.aspx");
            }
           
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {
                CutString Cutdate = new CutString();
                if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text != "")
                {
                    SerchDB loaddate = new SerchDB();
                    DataTable showdata = loaddate.GviewSumNotDay(TextBox3.Text, Convert.ToInt32(HidPenalty.Value), HidLateTime.Value);
                    if (showdata.Rows.Count == 0)
                    {
                        Warnning();
                    }
                    else
                    {
                        Session["GridViewDataSource"] = showdata;
                        Response.Redirect("ShowReport.aspx");
                    }
                }
               else if (TextBox1.Text != "" && TextBox2.Text == "" && TextBox3.Text == "")
                {
                    string startDateSerch = Cutdate.dtm(TextBox1.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                    string lastDateSerch = Cutdate.dtm(TextBox1.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                    SerchDB loaddate = new SerchDB();
                    DataTable showdata = loaddate.GviewSumDateOnly(startDateSerch, lastDateSerch, Convert.ToInt32(HidPenalty.Value), HidLateTime.Value);
                    if (showdata.Rows.Count == 0)
                    {
                        Warnning();
                    }
                    else
                    {
                        Session["GridViewDataSource"] = showdata;
                        Response.Redirect("ShowReport.aspx");
                    }
                }
                else if (TextBox1.Text == "" && TextBox2.Text != "" && TextBox3.Text == "")
                {
                    string startDateSerch = Cutdate.dtm(TextBox2.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                    string lastDateSerch = Cutdate.dtm(TextBox2.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                    SerchDB loaddate = new SerchDB();
                    DataTable showdata = loaddate.GviewSumDateOnly(startDateSerch, lastDateSerch, Convert.ToInt32(HidPenalty.Value), HidLateTime.Value);
                    if (showdata.Rows.Count == 0)
                    {
                        Warnning();
                    }
                    else
                    {
                        Session["GridViewDataSource"] = showdata;
                        Response.Redirect("ShowReport.aspx");
                    }
                }
                else if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text == "")
                {
                    string startDateSerch = Cutdate.dtm(TextBox1.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                    string lastDateSerch = Cutdate.dtm(TextBox2.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                    SerchDB loaddate = new SerchDB();
                    DataTable showdata = loaddate.GviewSumDateOnly(startDateSerch, lastDateSerch, Convert.ToInt32(HidPenalty.Value), HidLateTime.Value);
                    if (showdata.Rows.Count == 0)
                    {
                        Warnning();
                    }
                    else
                    {
                        Session["GridViewDataSource"] = showdata;
                        Response.Redirect("ShowReport.aspx");
                    }
                }
                else if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text == "")
                {
                    SerchDB loaddate = new SerchDB();
                    DataTable showdata = loaddate.GviewSumAllData(Convert.ToInt32(HidPenalty.Value),HidLateTime.Value);
                    if (showdata.Rows.Count == 0)
                    {
                        Warnning();
                    }
                    else
                    {
                        Session["GridViewDataSource"] = showdata;
                        Response.Redirect("ShowReport.aspx");
                        
                    }
                }
                else if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "")
                {

                    string startDateSerch = Cutdate.dtm(TextBox1.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                    string lastDateSerch = Cutdate.dtm(TextBox2.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                    SerchDB gviewSum = new SerchDB();
                    DataTable showdata = gviewSum.GviewSum(startDateSerch, lastDateSerch, TextBox3.Text, Convert.ToInt32(HidPenalty.Value), HidLateTime.Value);
                    if (showdata.Rows.Count == 0)
                        {
                            Warnning();
                        }
                    else
                        {
                            Session["GridViewDataSource"] = showdata;
                            Response.Redirect("ShowReport.aspx");
                            
                        }
                }
                else
                {
                    lb_warn.Visible = true;
                    GridView1.Visible = false;
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
            
        }

        private void Warnning()
        {
            lb_warn.Visible = true;
            GridView1.Visible = false;
        }

        protected void ImageExcel_Click(object sender, ImageClickEventArgs e)
        {
            
            
                
            
            }//button

        
        //Export To Excel-----------------------------------------------------------------------//
        public override void VerifyRenderingInServerForm(Control control) { }

        private void GridView1Excal(DataTable dt)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=ExportTime.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView1.DataSource = dt;
            GridView1.Columns[GridView1.HeaderRow.Cells.Count - 1].Visible = false;
            GridView1.DataBind();
            GridView1.AllowPaging = false;
            GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
            for (int k = 0; k < GridView1.HeaderRow.Cells.Count; k++)
            {
                GridView1.HeaderRow.Cells[k].Style.Add("background-color", "#0099FF");

            }
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {

                GridViewRow row = GridView1.Rows[i];

                //Change Color back to white

                row.BackColor = System.Drawing.Color.White;

                //Apply text style to each Row

                row.Attributes.Add("class", "textmode");
                for (int j = 0; j < GridView1.HeaderRow.Cells.Count - 1; j++)
                {
                    if (i % 2 != 0)
                    {
                        row.Cells[j].Style.Add("background-color", "#33CCFF");

                    }

                }



            }

            GridView1.RenderControl(hw);

            //style to format numbers to string

            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }

        private void BindGridView()
        {
            SerchDB loaddate = new SerchDB();
            DataTable showdata = loaddate.SearchALL();
            if (showdata.Rows.Count == 0)
            {
                Warnning();
            }
            else
            {
                GridView1.DataSource = showdata;
                GridView1.DataBind();
                lb_warn.Visible = false;
                GridView1.Visible = true;
            }
                  
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdEdit")
            {
                string emp = e.CommandArgument.ToString();
                Session["EditTime"] = emp;
                ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('EditTime.aspx','popup','width=800,height=80');</script>"));
                
            }
            if (e.CommandName == "cmdDel")
            {   
                CutString Cutdate = new CutString();
                int id = Convert.ToInt32(Cutdate.cutEMP(e.CommandArgument.ToString()));
                DateTime emp = Cutdate.dtm(Cutdate.cutDate(e.CommandArgument.ToString()),Cutdate.cutCheckIn(e.CommandArgument.ToString()));
                InSertData Deldata = new InSertData();
                Deldata.DelCheckIn(id,emp);
                BindGridView();
            }
            if (e.CommandName == "cmdSum")
            {
                string NameEmp = e.CommandArgument.ToString();
                TextBox3.Text = NameEmp;
                if (Session["User"] != null && Session["Pass"] != null)
                {
                    CutString Cutdate = new CutString();
                    if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text != "")
                    {
                        SerchDB loaddate = new SerchDB();
                        DataTable showdata = loaddate.GviewSumNotDay(TextBox3.Text, Convert.ToInt32(HidPenalty.Value), HidLateTime.Value);
                        if (showdata.Rows.Count == 0)
                        {
                            Warnning();
                        }
                        else
                        {
                            Session["GridViewDataSource"] = showdata;
                            Response.Redirect("ShowReport.aspx");
                        }
                    }
                    
                    else if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "")
                    {

                        string startDateSerch = Cutdate.dtm(TextBox1.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        string lastDateSerch = Cutdate.dtm(TextBox2.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        SerchDB gviewSum = new SerchDB();
                        DataTable showdata = gviewSum.GviewSum(startDateSerch, lastDateSerch, TextBox3.Text, Convert.ToInt32(HidPenalty.Value), HidLateTime.Value);
                        if (showdata.Rows.Count == 0)
                        {
                            Warnning();
                        }
                        else
                        {
                            Session["GridViewDataSource"] = showdata;
                            Response.Redirect("ShowReport.aspx");

                        }
                    }
                    else
                    {
                        SerchDB loaddate = new SerchDB();
                        DataTable showdata = loaddate.GviewSumNotDay(NameEmp, Convert.ToInt32(HidPenalty.Value), HidLateTime.Value);
                        if (showdata.Rows.Count == 0)
                        {
                            Warnning();
                        }
                        else
                        {
                            Session["GridViewDataSource"] = showdata;
                            Response.Redirect("ShowReport.aspx");
                        }
                    }
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }








            }

        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='#6699FF';";
                e.Row.Attributes["onmouseout"] = "this.style.cursor='hand';this.style.backgroundColor='white';";

                // e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {
                Response.Redirect("ShowAddtime.aspx");
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {
                CutString Cutdate = new CutString();
                if (GridView1.Visible == true && GridView1.Rows.Count != 0) //GridView1
                {
                    if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text == "")
                    {
                        SerchDB loaddate = new SerchDB();
                        DataTable dt = loaddate.SearchALL();
                        GridView1Excal(dt);
                    }
                    else if (TextBox1.Text != "" && TextBox2.Text == "" && TextBox3.Text == "")
                    {
                        string startDateSerch = Cutdate.dtm(TextBox1.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        string lastDateSerch = Cutdate.dtm(TextBox1.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        SerchDB loaddate = new SerchDB();
                        DataTable dt = loaddate.DateOnly(startDateSerch, lastDateSerch);
                        GridView1Excal(dt);
                    }
                    else if (TextBox1.Text == "" && TextBox2.Text != "" && TextBox3.Text == "")
                    {
                        string startDateSerch = Cutdate.dtm(TextBox2.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        string lastDateSerch = Cutdate.dtm(TextBox2.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        SerchDB loaddate = new SerchDB();
                        DataTable dt = loaddate.DateOnly(startDateSerch, lastDateSerch);
                        GridView1Excal(dt);
                    }
                    else if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text == "")
                    {
                        string startDateSerch = Cutdate.dtm(TextBox1.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        string lastDateSerch = Cutdate.dtm(TextBox2.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        SerchDB loaddate = new SerchDB();
                        DataTable dt = loaddate.DateOnly(startDateSerch, lastDateSerch);
                        GridView1Excal(dt);
                    }
                    else if (TextBox1.Text == "" && TextBox2.Text == "" && TextBox3.Text != "")
                    {
                        SerchDB loaddate = new SerchDB();
                        DataTable dt = loaddate.NotDay(TextBox3.Text);
                        GridView1Excal(dt);
                    }
                    else if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "")
                    {
                        string startDateSerch = Cutdate.dtm(TextBox1.Text, "").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        string lastDateSerch = Cutdate.dtm(TextBox2.Text, "23:59:59").ToString("yyyy-MM-dd HH:mm", new System.Globalization.CultureInfo("en-US"));
                        SerchDB gviewdb = new SerchDB();
                        DataTable dt = gviewdb.Gviewdb(startDateSerch, lastDateSerch, TextBox3.Text);
                        GridView1Excal(dt);
                    }
                    else
                    {
                        lb_warn.Visible = true;
                    }

                }
                else
                {
                    Response.Write("<script>alert('ไม่มีข้อมูลที่จะทำการ Export');</script>");
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }


        }
     

    }
